/*

 * Copyright (C) 2021 Huawei Device Co., Ltd.

 * Licensed under the Apache License, Version 2.0 (the "License");

 * you may not use this file except in compliance with the License.

 * You may obtain a copy of the License at

 *

 *     http://www.apache.org/licenses/LICENSE-2.0

 *

 * Unless required by applicable law or agreed to in writing, software

 * distributed under the License is distributed on an "AS IS" BASIS,

 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

 * See the License for the specific language governing permissions and

 * limitations under the License.

 */
package com.chinalwb.are.utils;


import ohos.agp.components.Text;
import ohos.agp.text.Font;
import ohos.agp.text.RichText;
import ohos.agp.text.RichTextBuilder;
import ohos.agp.text.TextForm;

import java.util.*;


/**
 * NoFieldTextDecorator
 *
 * @since 2021/06/22
 */
public class NoFieldTextDecorator {
    /**
     * 设置字体斜体
     */
    public static final int TYPE_ITALIC = 1001;
    /**
     * 设置字体加粗
     */
    public static final int TYPE_BOLD = 1002;

    private static final int INDEX = 0;
    private static final int UNDERLINE = 1;
    private static final int TEXT_COLOR = 2;
    private static final int TEXT_BAG = 3;
    private static final int STRIKETHROUGH = 4;
    private static final int SUBSCRIPT = 5;
    private static final int SUPERSCRIPT = 6;
    private static final int SCALE_X = 7;
    private static final int ABSOLUTE_SIZE = 8;
    private static final int RELATIVE_SIZE = 9;
    private static final String REPLACE = "TEMP替换符";
    private static final String AE8668EA = "ae8668ea";
    /**
     * 文本控件
     */
    private final Text textView;
    /**
     * 文本内容
     */
    private final String content;
    /**
     * 备用替换文本
     */
    private String tempContent;
    /**
     * 是否对文本中每个设置的text进行设置样式
     */
    private final boolean isContainAllText;
    /**
     * 字符串所在下标 集合
     */
    private final List<Integer> indexList;
    /**
     * 下标 字符串 键值对 集合
     */
    private final Map<Integer, String> indexTextMap;
    /**
     * 下标 富文本对象 键值对 集合
     */
    private final Map<Integer, TextForm> indexTextFormMap;

    private final RichTextBuilder richTextBuilder;
    private int mResTextColorId;
    private int mResTextBagColorId;
    private float mProportion;
    private float mRelativeSize;
    private int mTextSize;
    private Font mFontBold;
    private Font mFontItalic;
    private int index = INDEX;

    private NoFieldTextDecorator(Text textView, String content, boolean isContainAllText) {
        this.textView = textView;
        this.content = content;
        this.tempContent = content;
        this.isContainAllText = isContainAllText;

        indexList = new ArrayList<>();
        indexTextMap = new HashMap<>();
        indexTextFormMap = new HashMap<>();
        richTextBuilder = new RichTextBuilder();
    }

    /**
     * 初始化相关设置
     *
     * @param textView         文本控件
     * @param content          文本内容
     * @param isContainAllText 是否查找文本中所有包含设置字符串的内容
     * @return NoFieldTextDecorator
     */
    public static NoFieldTextDecorator decorate(Text textView, String content, boolean isContainAllText) {
        return new NoFieldTextDecorator(textView, content, isContainAllText);
    }

    /**
     * 设置下划线样式
     *
     * @param texts 设置的字符串
     * @return NoFieldTextDecorator
     */
    public NoFieldTextDecorator underline(final String... texts) {
        setData(UNDERLINE, new TextForm().setUnderline(true), texts);
        return this;
    }

    /**
     * 设置文本颜色样式
     *
     * @param resColorId 设置的字体颜色
     * @param texts      设置的字符串
     * @return NoFieldTextDecorator
     */
    public NoFieldTextDecorator setTextColor(final int resColorId, final String... texts) {
        this.mResTextColorId = resColorId;
        setData(TEXT_COLOR, new TextForm().setTextColor(resColorId), texts);
        return this;
    }

    /**
     * 设置文本背景色
     *
     * @param colorResId 设置的字体背景色
     * @param texts      设置的字符串
     * @return NoFieldTextDecorator
     */
    public NoFieldTextDecorator setBackgroundColor(final int colorResId, final String... texts) {
        this.mResTextBagColorId = colorResId;
        setData(TEXT_BAG, new TextForm().setTextBackgroundColor(colorResId), texts);
        return this;
    }

    /**
     * 设置文本删除线
     *
     * @param texts 设置的字符串
     * @return NoFieldTextDecorator
     */
    public NoFieldTextDecorator strikethrough(final String... texts) {
        setData(STRIKETHROUGH, new TextForm().setStrikethrough(true), texts);
        return this;
    }

    /**
     * 设置文本下标
     *
     * @param texts 设置的字符串
     * @return NoFieldTextDecorator
     */
    public NoFieldTextDecorator setSubscript(final String... texts) {
        setData(SUBSCRIPT, new TextForm().setSubscript(true), texts);
        return this;
    }

    /**
     * 设置文本上标
     *
     * @param texts 设置的字符串
     * @return NoFieldTextDecorator
     */
    public NoFieldTextDecorator setSuperscript(final String... texts) {
        setData(SUPERSCRIPT, new TextForm().setSuperscript(true), texts);
        return this;
    }

    /**
     * 设置字体样式
     *
     * @param type  字体样式
     * @param texts 设置的字符串
     * @param isBold 是否是粗体
     * @return NoFieldTextDecorator
     */
    public NoFieldTextDecorator setFont(int type, boolean isBold, final String... texts) {
        int indexS = INDEX;
        for (String text : texts) {
            Font.Builder fontBuilder = new Font.Builder(text);
            if (type == TYPE_ITALIC) {
                fontBuilder.makeItalic(true);
            }
            if (isBold) {
                fontBuilder.setWeight(Font.BOLD);
            }
            this.mFontItalic = fontBuilder.build();
            if (content.contains(text)) {
                // 定义循环,如果index的位置不是-1,就一值查找
                while ((indexS = content.indexOf(text, indexS)) != -1) {
                    if (!indexList.contains(indexS)) {
                        indexList.add(indexS);
                        indexTextMap.put(indexS, text);
                        indexTextFormMap.put(indexS, new TextForm().setTextFont(mFontItalic));
                    } else {
                        setTextForm(TYPE_ITALIC, indexTextFormMap.get(indexS));
                    }
                    // 每循环一次就要明确下一次查找的位置
                    indexS = indexS + text.length();
                }
            }
        }
        return this;
    }

    /**
     * 设置字体样式
     *
     * @param font  字体样式对象
     * @param texts 设置的字符串
     * @return NoFieldTextDecorator
     */
    public NoFieldTextDecorator setFont(Font font, final String... texts) {
        this.mFontBold = font;
        setData(TYPE_BOLD, new TextForm().setTextFont(font), texts);
        return this;
    }

    /**
     * 设置文本绝对字体大小
     *
     * @param size  字体大小
     * @param texts 设置的字符串
     * @return NoFieldTextDecorator
     */
    public NoFieldTextDecorator setAbsoluteSize(final int size, final String... texts) {
        this.mTextSize = size;
        setData(ABSOLUTE_SIZE, new TextForm().setTextSize(size), texts);
        return this;
    }

    /**
     * 设置文本相对字体大小
     *
     * @param proportion 相对字体大小
     * @param texts      设置的字符串
     * @return NoFieldTextDecorator
     */
    public NoFieldTextDecorator setRelativeSize(final float proportion, final String... texts) {
        this.mRelativeSize = proportion;
        setData(RELATIVE_SIZE, new TextForm().setRelativeTextSize(proportion), texts);
        return this;
    }

    /**
     * 设置文本缩放样式
     *
     * @param proportion 缩放大小
     * @param texts      设置的字符串
     * @return NoFieldTextDecorator
     */
    public NoFieldTextDecorator scaleX(final float proportion, final String... texts) {
        this.mProportion = proportion;
        setData(SCALE_X, new TextForm().setScaleX(proportion), texts);
        return this;
    }

    private void setData(int type, TextForm textForm, final String... texts) {
        for (String text : texts) {
            if (content.contains(text)) {
                // 拿到字符串 在文本中找到所有包含字符串的下标
                // 自定义循环如果index的位置不是-1 就一直查找
                while ((index = content.indexOf(text, index)) != -1) {
                    if (!indexList.contains(index)) {
                        indexList.add(index);
                        indexTextMap.put(index, text);
                        indexTextFormMap.put(index, textForm);
                    } else {
                        setTextForm(type, indexTextFormMap.get(index));
                    }
                    // 每循环一次就要确定下一次查找的位置
                    index = index + text.length();
                    // 如果设置了不查找文本所有设置字符串即结束循环
                    if (!isContainAllText) {
                        break;
                    }
                }
            }
        }
    }

    private void setTextForm(int type, TextForm textForm) {
        switch (type) {
            case UNDERLINE:
                textForm.setUnderline(true);
                break;
            case TEXT_COLOR:
                textForm.setTextColor(mResTextColorId);
                break;
            case TEXT_BAG:
                textForm.setTextBackgroundColor(mResTextBagColorId);
                break;
            case STRIKETHROUGH:
                textForm.setStrikethrough(true);
                break;
            case SUBSCRIPT:
                textForm.setSubscript(true);
                break;
            case SUPERSCRIPT:
                textForm.setSuperscript(true);
                break;
            case SCALE_X:
                textForm.setScaleX(mProportion);
                break;
            case RELATIVE_SIZE:
                textForm.setRelativeTextSize(mRelativeSize);
                break;
            case ABSOLUTE_SIZE:
                textForm.setTextSize(mTextSize);
                break;
            case TYPE_ITALIC:
                textForm.setTextFont(mFontItalic);
                break;
            case TYPE_BOLD:
                textForm.setTextFont(mFontBold);
                break;
            default:
                break;
        }
    }


    /**
     * 创建富文本对象显示设置的文本相关样式
     */
    public void build() {
        int count = 0;
        List<String> list = sort();
        // 替换
        for (String string : list) {
            if (string.equals(REPLACE)) {
                richTextBuilder.mergeForm(indexTextFormMap.get(indexList.get(count)));
                richTextBuilder.addText(indexTextMap.get(indexList.get(count)));
                richTextBuilder.revertForm();
                count++;
            } else {
                richTextBuilder.addText(string);
                richTextBuilder.revertForm();
            }
        }
        RichText richText = richTextBuilder.build();
        textView.setRichText(richText);
    }

    private List<String> sort() {
        int temp;
        // for循环选取第一个数
        for (int i = 0; i < indexList.size() - 1; i++) {
            // 原数组中，取出第一个数之后，相邻的另外一个数
            for (int j = 0; j < indexList.size() - 1 - i; j++) {
                // 比较两个整数的大小
                if (indexList.get(j) > indexList.get(j + 1)) {
                    temp = indexList.get(j);
                    indexList.set((j), indexList.get(j + 1));
                    indexList.set(j + 1, temp);
                }
            }
        }
        return replace();
    }

    private List<String> replace() {
        for (int id : indexList) {
            tempContent = tempContent.replace(indexTextMap.get(id), AE8668EA + REPLACE + AE8668EA);
            textView.setText(tempContent);
        }
        String[] strings = tempContent.split(AE8668EA);
        return Arrays.asList(strings);
    }
}
